// Created by WXX on 2021/9/17 10:54
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：4 ms, 在所有 C++ 提交中击败了42.90%的用户
 * 内存消耗：6.1 MB, 在所有 C++ 提交中击败了91.29%的用户
 */
class Solution {
public:
    string validIPAddress(string ip) {
        if (ip.find(".") != -1 && ip.find(':') != -1) return "Neither";
        if (ip.find('.') != -1) return check_ipv4(ip);
        if (ip.find(':') != -1) return check_ipv6(ip);
        return "Neither";
    }

    vector<string> split(string ip, char c) {
        vector<string> res;
        for (int i = 0; i < ip.size(); i++) {
            int j = i;
            string item;
            while (ip[j] != c) item += ip[j++];
            i = j;
            res.push_back(item);
        }
        return res;
    }

    string check_ipv4(string ip) {
        vector<string> items = split(ip + '.', '.');
        if (items.size() != 4) return "Neither";
        for (auto item : items) {
            if (item.empty() || item.size() > 3) return "Neither";
            if (item.size() > 1 && item[0] == '0') return "Neither";
            for (auto c : item)
                if (c < '0' || c > '9') return "Neither";
            int t = stoi(item);
            if (t > 255) return "Neither";
        }
        return "IPv4";
    }

    string check_ipv6(string ip) {
        vector<string> items = split(ip + ':', ':');
        if (items.size() != 8) return "Neither";
        for (auto item : items) {
            if (item.empty() || item.size() > 4) return "Neither";
            for (auto c : item)
                if (!check(c))
                    return "Neither";
        }
        return "IPv6";
    }

    bool check(char c) {
        if (c >= '0' && c <= '9') return true;
        if (c >= 'a' && c <= 'f') return true;
        if (c >= 'A' && c <= 'F') return true;
        return false;
    }
};

int main() {


    return 0;
}
